//
//  AlertBootcamp.swift
//  FCSwiftUI
//
//  Created by 冯宣超 on 2023/2/26.
//

import SwiftUI

struct AlertBootcamp: View {
    
    @State var showAlert:Bool = false
    @State var backgroudColor: Color = Color.yellow
    @State var alertType: MyAlerts? = nil
    //@State var alertTitle: String = ""
    //@State var alertMessage: String = ""
    
    enum MyAlerts {
        case success
        case error
    }
    
    var body: some View {
        ZStack {
            backgroudColor.edgesIgnoringSafeArea(.all)
            
            VStack {
                Button("BUTTON 1") {
                    //alertTitle = "ERROR UPLOADING VIDEO"
                    //alertMessage = "The vidio could not be uploaded"
                    alertType = .error
                    showAlert.toggle()
                }
                
                Button("BUTTON 2") {
                    //alertTitle = "Successfully uploaded video 🥳"// control + command + 空格键 = 内置表情😊
                    //alertMessage = "The vidio is now pubic!"
                    alertType = .success
                    showAlert.toggle()
                }

            }
            .alert(isPresented: $showAlert) {
                getAlert()
            }
        }
    }
    
    func getAlert() -> Alert {
//        Alert(
//            title: Text("This is the title"),
//            message: Text("Here we wil describe the error."),
//            primaryButton: .destructive(Text("DELETE"), action: {
//                backgroudColor = .red
//            }),
//            secondaryButton: .cancel()
//        )
        
//        Alert(
//            title: Text(alertTitle),
//            message: Text(alertMessage),
//            dismissButton: .default(Text("OK"))
//        )
        
        switch alertType {
        case .error:
            return Alert(title: Text("There was an error!"))
        case .success:
            return Alert(title: Text("There was a success!"), message: nil, dismissButton: .default(Text("OK"), action: {
                backgroudColor = .green
            }))
        default:
            return Alert(title: Text("ERROR"))
        }
    }
}

struct AlertBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        AlertBootcamp()
    }
}
